package com.app.service.impl;

import com.app.mapper.ElectronicMediaMapper;
import com.app.model.ElectronicMediaModel;
import com.app.service.ElectronicMediaService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Primary
public class ElectronicMediaServiceImpl implements ElectronicMediaService {
   @Autowired
    ElectronicMediaMapper electronicMediaMapper;


    @Override
    public ElectronicMediaModel getElcMediaById(int id) {
        QueryWrapper<ElectronicMediaModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        ElectronicMediaModel role =  electronicMediaMapper.selectOne(queryWrapper);
        if (role == null) {
            return null;
        } else {
            return role;
        }
    }

    @Override
    public ElectronicMediaModel getElcMediaByPersonId(Integer personId,Integer mediaType) {
        QueryWrapper<ElectronicMediaModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("person_id", personId).eq("media_type",mediaType);
        ElectronicMediaModel role =  electronicMediaMapper.selectOne(queryWrapper);
        return role;
    }

    @Override
    public Page<ElectronicMediaModel> getPageElcMediaList(int page, int limit, String mediaNumber, List<Integer> personIdList, Integer sortType) {
        QueryWrapper<ElectronicMediaModel> queryWrapper = new QueryWrapper<>();
        if(personIdList == null || personIdList.size() == 0){
            queryWrapper.select();
        }else {
            queryWrapper.select().in("person_id",personIdList);
        }

        if(mediaNumber != null){
            queryWrapper.like("media_number",mediaNumber);
        }
        if (sortType==null || sortType==1){
            queryWrapper.orderByAsc("number").orderByDesc("create_time");
        }else {
            queryWrapper.orderByDesc("number").orderByDesc("create_time");
        }
        Page<ElectronicMediaModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<ElectronicMediaModel> roleList = electronicMediaMapper.selectPage(pages,queryWrapper);
        return roleList;
    }


    @Override
    public void update(ElectronicMediaModel model) {
        electronicMediaMapper.updateById(model);
    }

    @Override
    public void insert(ElectronicMediaModel model) {
        electronicMediaMapper.insert(model);
    }


    @Override
    public void delete(int id) {
        electronicMediaMapper.deleteById(id);
    }

    @Override
    public List<ElectronicMediaModel> selectElcMediaByPersonId(Integer personId) {
        QueryWrapper<ElectronicMediaModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("person_id", personId);
        return electronicMediaMapper.selectList(queryWrapper);
    }
//批量删除社交媒体账号
    @Override
    public Result<Object> deleteMediaBatchByIds(String ids) {
        // 参数校验
        if (StringUtils.isEmpty(ids)) {
            return Result.success("参数为空，无需删除");
        }
        List<Integer> idsList = Arrays.stream(ids.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        // 批量删除
        if (!ListUtils.isEmpty(idsList)) {
            electronicMediaMapper.deleteBatchIds(idsList);
            return Result.success("批量删除成功");
        } else {
            return Result.success("批量删除失败，数据为空");
        }
    }
}
